<template>
	<view class="main-body flex flex-col">
		<div class="flex flex-col flex-auto overflow-auto">
			<div  v-if="act=='check'">
				<h2>隐患验收</h2>
				<u-form :model="handleForm" ref="handleForm">
					
					<u-form-item label="验收状况" label-width="labelWidth" required>
						<u-input v-model="handleForm.danger_status_name" @click="dangerStatusShow = true" disabled />
						<u-select v-model="dangerStatusShow" :list="danger_status_list" @confirm="dangerStatusConfirm"></u-select>
					</u-form-item>
					<u-form-item label="验收备注" label-width="labelWidth" required>
						<template>
							<u-input type="textarea" v-model="handleForm.remark" />
						</template>
					</u-form-item>
				</u-form>
			</div>
			<div  v-if="act=='handle'">
				<h2>隐患整改</h2>
				<u-form :model="handleForm" ref="handleForm">
					<u-form-item label="整改图片" label-width="labelWidth" required>
						<u-upload :action="handleAction" ref="handleUpload" :max-count="4"></u-upload>
					</u-form-item>
					<u-form-item label="整改视频" label-width="labelWidth" required>
						<div class="up-btn" @click="upVideo" v-if="!handleForm.resources_video">
							<div class="text">上传视频</div>
						</div>
						<div class="upload-main">
							<video v-if="handleForm.resources_video" :src="handleForm.resources_video" controls></video>
						</div>
					</u-form-item>
					<u-form-item label="整改备注" label-width="labelWidth" required>
						<template>
							<u-input type="textarea" v-model="handleForm.remark" />
						</template>
					</u-form-item>
				</u-form>
			</div>
			
			<u-form :model="form" ref="uForm" >
				<template v-if="act=='assign'||act=='ransfert'">
					<h2>指派或转派</h2>
					<u-form-item label="隐患类型" label-width="labelWidth" required>
						<template>
							<!-- {{form.charge_user_id}} -->

							<u-input v-model="danger_cate_name" @click="cateShow = true" disabled />
							<u-select v-model="cateShow" :list="cate_list" @confirm="typeConfirm"></u-select>
						</template>

					</u-form-item>
					<u-form-item label="隐患负责人" label-width="labelWidth" required>
						<template>
							<u-input v-model="charge_user" disabled />
						</template>
					</u-form-item>
					<u-form-item label="隐患程度" label-width="labelWidth" required>
						<template>
							<u-input v-model="level_list[form.danger_level].label" @click="levelShow = true" disabled />
							<u-select v-model="levelShow" :list="level_list" @confirm="levelConfirm"
								:default-value="[1]">
							</u-select>
						</template>
					</u-form-item>
					<template v-if="form.charge_user_id==form.user_id">
						<u-form-item label="处理人" label-width="labelWidth" required prop="handle_user">
							<template>
								<u-input v-model="form.handle_user" @click="handleUserShow = true" disabled />
								<u-select v-model="handleUserShow" :list="handle_user_list"
									@confirm="handleUserConfirm">
								</u-select>
							</template>
						</u-form-item>
						<u-form-item label="处理时间" label-width="labelWidth" required>
							<template>
								<div class="flex">
									<div class="flex-1">
										<u-input v-model="form.need_finish_hour" @click="timeShow = true" disabled />
									</div>

									小时
								</div>
								<u-keyboard ref="uKeyboard" mode="number" v-model="timeShow" @change="valChange"
									@backspace="backspace"></u-keyboard>
							</template>
						</u-form-item>
						<u-form-item label="处理备注" label-width="labelWidth" required prop="do_remark">
							<template>
								<u-input type="textarea" v-model="form.do_remark" />
							</template>
						</u-form-item>
					</template>
				</template>

			</u-form>
		<Detail :eId="eId" :act="act"></Detail>
		</div>
		<div class="pt-2">
			<u-button @click="submit" type="primary" v-if="act=='assign'">指派</u-button>
			<u-button @click="handleSubmit" type="primary" v-if="act=='handle'">处理</u-button>
			<u-button @click="checkSubmit" type="primary" v-if="act=='check'">验收</u-button>
		</div>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		getCateList,
		getDetail,
		pointDanger,
		doDanger,
		acceptDanger
	} from "@/api/hidden";
	let base_url = 'https://safe.81game.top'
	import Detail from "./component/Detail.vue";
	export default {
		components: {
			Detail
		},
		data() {
			return {
				handleAction: base_url + '/api/file/upload_img.html',
				//act:handle处理，assign指派，ransfert转派,check验收
				act: '',
				eId:0,
				handleForm: {
					user_id: uni.getStorageSync('userInfo').id,
					org_id: uni.getStorageSync('userInfo').org_id,
					user_name: uni.getStorageSync('userInfo').nickname,
					resources:[],
					resources_video:'',
					remark:'',
					danger_id:0
				},
				form: {
					user_id: uni.getStorageSync('userInfo').id,
					org_id: uni.getStorageSync('userInfo').org_id,
					user_name: uni.getStorageSync('userInfo').nickname,
					position: '',
					danger_detail: '',
					danger_cate: 1,
					charge_user_id: '',
					resources: [],
					resources_video: '',
					danger_status: 0,
					danger_level: 0,
					handle_user: '',
					do_user_id: 0,
					need_finish_hour: '',
					do_remark: ""
				},
				rules: {
					handle_user: [{
						required: true,
						message: '请选择处理人',
						trigger: ['change', 'blur'],
					}],
					do_remark: [{
						required: true,
						message: '请输入处理备注',
						trigger: ['change', 'blur'],
					}],
					intro: [{
						min: 5,
						message: '简介不能少于5个字',
						trigger: 'change'
					}]
				},
				danger_cate_name: '',
				cateShow: false,
				levelShow: false,
				timeShow: false,
				dangerStatusShow: false,
				charge_user: '',
				cate_list: [],
				handle_user_list: [],
				handleUserShow: false,
				danger_status_list:[
					{
							value: '0',
							label: '不通过'
						}, {
							value: '1',
							label: '通过'
						}
				],
				danger_level: 0,
				danger_level_name: '',
				dangerflow: [],
				level_list: [{
						value: '0',
						label: ''
					}, {
						value: '1',
						label: '一般'
					},
					{
						value: '2',
						label: '较大'
					},
					{
						value: '3',
						label: '严重'
					},
					{
						value: '4',
						label: '重大'
					}

				]
			}
		},
		onLoad(obj) {
			this.danger_id = obj.id
			this.act = obj.act
			this.eId = parseInt(obj.id)
			this.getDetail()
			this.getCateList()
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			submit() {
				this.$refs.uForm.validate(valid => {
					console.log(111, valid)
					if (valid) {
						pointDanger(this.form).then(res => {
							this.$refs.uToast.show({
								title: '指派成功',
								type: 'success',
								url: '/pages/hidden/hidden'
							})
						})
					} else {
						console.log('验证失败');
					}
				});
			},
			handleSubmit() {
				this.getFileList()
				this.handleForm.danger_id = this.eId
				console.log(55,this.handleForm)
				doDanger(this.handleForm).then(res => {
					this.$refs.uToast.show({
						title: '提交成功',
						type: 'success',
						url: '/pages/hidden/hidden'
					})
				})
			},
			checkSubmit() {
				this.handleForm.danger_id = this.eId
				console.log(55,this.handleForm)
				acceptDanger(this.handleForm).then(res => {
					this.$refs.uToast.show({
						title: '提交成功',
						type: 'success',
						url: '/pages/hidden/hidden'
					})
				})
			},
			getFileList() {
				let files = [];
				files = this.$refs.handleUpload.lists.filter(val => {
					return val.progress == 100;
				})
				console.log(6,files)
				let newList = this.makeFileList(files)
			
				this.handleForm.resources = newList
			},
			makeFileList(files) {
				let list = []
				files.forEach(function(value, index, array) {
					// list.push(value.response.data)
					list.push({
						"res_url": value.response.data.src,
						'res_type': 1,
						'res_ext': value.response.data.src.substr(value.response.data.src.lastIndexOf(
							".") + 1)
					})
				});
				return list
			},
			typeConfirm(e) {
				let index = e[0].value - 1;
				this.form.danger_cate = e[0].value
				this.form.charge_user_id = this.cate_list[index].charge_user_id
				this.charge_user = this.cate_list[index].charge_user
				this.danger_cate_name = e[0].label
			},
			dangerStatusConfirm(e) {
				let index = e[0].value - 1;
				this.handleForm.danger_status = e[0].value
				this.handleForm.danger_status_name = e[0].label
			},
			levelConfirm(e) {
				let index = e[0].value - 1;
				this.form.danger_level = e[0].value
				this.danger_level_name = e[0].label
			},
			userConfirm(e) {
				let index = e[0].value - 1;
				console.log('a', index)
			},
			handleUserConfirm(e) {
				console.log(111)
				this.form.do_user_id = e[0].value
				this.form.handle_user = e[0].label
			},
			getDetail() {
				let that = this
				getDetail({
					user_id: uni.getStorageSync('userInfo').id,
					org_id: uni.getStorageSync('userInfo').org_id,
					danger_id: this.danger_id
				}).then(res => {
					
					// that.form = that.form.concat(res.data.data.dangerinfo)
					that.form = {
						...that.form,
						...res.data.data.dangerinfo
					}
					console.log(666, that.form)
					this.dangerflow = res.data.data.dangerflow
					// this.danger_cate_name = 
					// this.imgLists = res.data.data.dangerinfo.up_imgs
					// this.imgLists = this.$refs.uUpload.lists;
					let temp = []
					res.data.data.dangerinfo.up_imgs.forEach((item, index) => {
						temp.push({
							url: item.res_url
						})
						console.log(1, item)
					})
					// this.$refs.uUpload.lists = temp
					// this.form.resources_video = res.data.data.dangerinfo.up_video
				})
			},
			getCateList() {
				let that = this
				getCateList({
					org_id: uni.getStorageSync('userInfo').org_id,
				}).then(res => {
					console.log(11555, res.data.data)
					let cateList = []
					res.data.data.forEach(function(item, index, array) {
						that.danger_cate_name = res.data.data[that.form.danger_cate].cate_name
						that.charge_user = res.data.data[that.form.danger_cate].charge_user
						cateList.push({
							"value": item.cate_id,
							"label": item.cate_name,
							"charge_user_id": item.charge_user_id,
							"charge_user": item.charge_user,
						})
						if (item.charge_user_id == that.form.user_id) {
							var h_user_list = []

							item.team_info.forEach(function(m, i) {
								h_user_list.push({
									"value": m.user_id,
									"label": m.nickname,
								})
							})
							console.log(222, h_user_list)
							that.handle_user_list = h_user_list
						}
					})
					this.cate_list = cateList
				})
			},
			upVideo() {
				var self = this;
				uni.chooseVideo({
					count: 1,
					sourceType: ['camera', 'album'],
					success: function(res) {
			
						uni.showLoading({
							title: '正在上传视频...'
						});
						self.videoFile = res.tempFilePath;
						uni.uploadFile({
							url: base_url + '/api/file/upload_video.html',
							// url: 'http://safe.cqjhhr.com/api/file/upload_video',
							filePath: res.tempFilePath,
							name: 'file',
							formData: {
								'user': 'test'
							},
							success: (uploadFileRes) => {
								let ute = JSON.parse(uploadFileRes.data)
								self.handleForm.resources_video = ute.data.src
								uni.hideLoading()
							}
						});
					}
				});
			},
			valChange(val) {
				// 将每次按键的值拼接到value变量中，注意+=写法
				if (this.form.need_finish_hour == "0") {
					this.form.need_finish_hour = ''
				}

				this.form.need_finish_hour += val;
			},
			// 退格键被点击
			backspace() {
				// 删除value的最后一个字符
				if (this.form.need_finish_hour.length) this.form.need_finish_hour = this.form.need_finish_hour.substr(0,
					this.form.need_finish_hour.length - 1);
				console.log(this.form.need_finish_hour);
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import './hidden.scss'
</style>
